
public class P36 {
	public static int max(int n) {
		int i = 0;
		
		while (Math.pow(2, i) <= n) {
			i++;
		}
		
		return i - 1;
	}
	
	public static String toBinary(int n) {
		StringBuffer binary = new StringBuffer();
		
		int max = max(n);
		
		for (int i = max; i >= 0; i--) {
			int pow = (int)Math.pow(2, i);
			binary.append(n/pow);
			n = n%pow;
		}
		
		return binary.toString();
	}
	
	public static void main(String[] args) {
		System.out.println(max(63));
		System.out.println(max(64));
		System.out.println(toBinary(63));
		System.out.println(toBinary(64));
		
		int sum = 0;
		for (int i = 1; i < 1000000; i++) {
			if (String.valueOf(i).equals(new StringBuffer().append(i).reverse().toString())) {
				String binary = toBinary(i);
				if (binary.equals(new StringBuffer().append(binary).reverse().toString())) {
					sum += i;
				}
				
			}
		}
		
		System.out.println(sum);
				
	}	
}
